/*
 * BanBu_ProgressController
 * @description 本文件提供相册文件上传的进度条显示
 * @package
 * @author 		yinlinlin
 * @copyright
 * @version 		1.0
 * @description 本文件提供相册文件上传的进度条显示
 */


#import "BanBu_ProgressController.h"
#import "AppCommunicationManager.h"
#import "BanBu_ProgressCell.h"
@interface BanBu_ProgressController ()

@end

@implementation BanBu_ProgressController
@synthesize updateArr = _updateArr;

- (void)dealloc
{
    self.updateNumChangeCallBack = nil;
    [_updateArr release];
    _updateArr = nil;
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
    [self.tableView reloadData];
    [super viewWillAppear:animated];
}
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _updateArr = [[NSMutableArray alloc]init];
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = NSLocalizedString(@"album_updateQueue",上传队列);//上传队列
}

#pragma mark - 上传队列的单例
/**
 * @函数名称：sharedManager
 * @函数描述：上传队列的单例
 * @输入参数：void
 * @输出参数：单例：BanBu_ProgressController
 * @返回值：void
 * @author：yinlinlin
 */
//上传单例
+ (BanBu_ProgressController *)sharedManager
{
    static BanBu_ProgressController *sharedAccountManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedAccountManagerInstance = [[self alloc] init];
    });
    return sharedAccountManagerInstance;
}

#pragma mark - 添加上传数据
/**
 * @函数名称：addUpdateDate
 * @函数描述：将要上传的数据加到_updateArr中
 * @输入参数：(NSDictionary *)updateDic
 * @输出参数：void
 * @返回值：void
 * @author：yinlinlin
 */
- (void)addUpdateDate:(NSDictionary *)updateDic
{
    NSLog(@"上传的数据:%@",updateDic);
    [_updateArr addObject:updateDic];
    if (_updateNumChangeCallBack)
    {
        _updateNumChangeCallBack(_updateArr.count);
    }
    [self.tableView reloadData];
    
}




#pragma mark - ASIProgressDelegate
/**
 * @函数名称：setProgress
 * @函数描述：执行ASIProgressDelegate的代理方法
 * @输入参数：void
 * @输出参数：void
 * @返回值：void
 * @author：yinlinlin
 */
- (void)setProgress:(float)newProgress
{
    NSLog(@"newProgress:%f",newProgress);
    NSLog(@"正在上传的数量AppComManager:%d",AppComManager.updateAlbumdataQueue.requestsCount);
    NSLog(@"self正在上传数量:%d",_updateArr.count);
    
    //判断当队列执行的请求数量：AppComManager.updateAlbumdataQueue.requestsCount < _updateArr.count当前记录的请求数量，说明返回的newProgress是当前需要显示的，因为newProgress返回为1.0的时候比请求数量要多
    //判断AppComManager.updateAlbumdataQueue.requestsCount > 1 是因为请求数量为1时，请求完成之后，请求数量有可能不会减到1
    if ((AppComManager.updateAlbumdataQueue.requestsCount >= _updateArr.count)&&(AppComManager.updateAlbumdataQueue.requestsCount > 1))
    {
        return;
    }
    if ((int)newProgress == 1)
    {
        return;
    }
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    BanBu_ProgressCell * cell = (BanBu_ProgressCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    [cell.progressIng setFrame:CGRectMake(0, 0, newProgress * 78, 15)];
    cell.progressIng.image = [[UIImage imageNamed:@"progressImg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(6.0, 6.0, 6.0, 6.0)];
    cell.progressLa.text = [NSString stringWithFormat:@"%@%d%@",NSLocalizedString(@"album_updateing", 正在上传),(int)(newProgress * 100),@"%"];
    NSLog(@"%d",(int)newProgress);
//    if ((int)newProgress == 1)
//    {
//        if (_updateArr.count > 0)
//        {
//            [_updateArr removeObjectAtIndex:0];
//            if (_updateNumChangeCallBack)
//            {
//                _updateNumChangeCallBack(_updateArr.count);
//            }
//            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
//        }
//        else
//        {
//            if (_updateNumChangeCallBack)
//            {
//                _updateNumChangeCallBack(0);
//            }
//        }
//        
//    }
}


#pragma mark - 上传成功
- (void)successToUpdate
{
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    BanBu_ProgressCell * cell = (BanBu_ProgressCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    [cell.progressIng setFrame:CGRectMake(0, 0, 78, 15)];
    cell.progressIng.image = [[UIImage imageNamed:@"progressImg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(6.0, 6.0, 6.0, 6.0)];
    cell.progressLa.text = [NSString stringWithFormat:@"%@100%@",NSLocalizedString(@"album_updateing", 正在上传),@"%"];

    
    if (_updateArr.count > 0)
    {
        
        [_updateArr removeObjectAtIndex:0];
        NSLog(@"_updateArr.count：%d",_updateArr.count);

//        if (_updateNumChangeCallBack)
//        {
//            _updateNumChangeCallBack(_updateArr.count);
//        }
//        //当数组减为0时，把section也要删除
//        if (_updateArr.count == 0)
//        {
//            NSIndexSet * indexSet = [NSIndexSet indexSetWithIndex:0];
//            [self.tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationRight];
//        }

        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
                NSLog(@"%d",_updateArr.count);
//        [self.tableView reloadData];
        
    }
    else
    {
        if (_updateNumChangeCallBack)
        {
            _updateNumChangeCallBack(0);
        }
    }

}
#pragma mark - 上传失败
/**
 * @函数名称：falseToUpdate
 * @函数描述：处理上传失败的数据，移除
 * @输入参数：void
 * @输出参数：void
 * @返回值：void
 * @author：yinlinlin
 */
- (void)falseToUpdate
{
    NSLog(@"失败移除");
    if (_updateArr.count > 0)
    {
        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [_updateArr removeObjectAtIndex:0];
        if (_updateNumChangeCallBack)
        {
            _updateNumChangeCallBack(_updateArr.count);
        }
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }
}
#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _updateArr.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identify = @"BanBu_ProgressCell";
    BanBu_ProgressCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell)
    {
        cell = [[[BanBu_ProgressCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify]autorelease];
    }
    NSDictionary * updateDic = [_updateArr objectAtIndex:indexPath.row];
    [cell updateProgressCellData:updateDic];
    cell.progressLa.text = [NSString stringWithFormat:@"%@0%@",NSLocalizedString(@"album_waiting", 等待上传),@"%"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 63;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
